package com.jeesite.modules.dp.web.kccode;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.jeesite.common.collect.ListUtils;
import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.lang.DateUtils;
import com.jeesite.common.utils.excel.ExcelExport;
import com.jeesite.common.utils.excel.annotation.ExcelField.Type;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.dp.entity.kccode.DpKechengcode;
import com.jeesite.modules.dp.service.kccode.DpKechengcodeService;

/**
 * 观看码管理Controller
 *
 * @author hancing.com
 * @version 2025-01-02
 */
@Controller
@RequestMapping(value = "${adminPath}/dp/kccode/dpKechengcode")
public class DpKechengcodeController extends BaseController {

    @Autowired
    private DpKechengcodeService dpKechengcodeService;

    /**
     * 获取数据
     */
    @ModelAttribute
    public DpKechengcode get(String id, boolean isNewRecord) {
        return dpKechengcodeService.get(id, isNewRecord);
    }

    /**
     * 查询列表
     */
    @RequiresPermissions("dp:kccode:dpKechengcode:view")
    @RequestMapping(value = {"list", ""})
    public String list(DpKechengcode dpKechengcode, Model model) {
        model.addAttribute("dpKechengcode", dpKechengcode);
        return "modules/dp/kccode/dpKechengcodeList";
    }

    /**
     * 查询列表数据
     */
    @RequiresPermissions("dp:kccode:dpKechengcode:view")
    @RequestMapping(value = "listData")
    @ResponseBody
    public Page<DpKechengcode> listData(DpKechengcode dpKechengcode, HttpServletRequest request, HttpServletResponse response) {
        dpKechengcode.setPage(new Page<>(request, response));
        Page<DpKechengcode> page = dpKechengcodeService.findPage(dpKechengcode);
        return page;
    }

    /**
     * 查看编辑表单
     */
    @RequiresPermissions("dp:kccode:dpKechengcode:view")
    @RequestMapping(value = "form")
    public String form(DpKechengcode dpKechengcode, Model model) {
        model.addAttribute("dpKechengcode", dpKechengcode);
        return "modules/dp/kccode/dpKechengcodeForm";
    }

    /**
     * 保存数据
     */
    @RequiresPermissions("dp:kccode:dpKechengcode:edit")
    @PostMapping(value = "save")
    @ResponseBody
    public String save(@Validated DpKechengcode dpKechengcode) {
        //
        String[] arrs = dpKechengcode.getKccode().split("\n");
        List<DpKechengcode> codes = new ArrayList<DpKechengcode>();
        for (String code : arrs) {
            DpKechengcode tmp = new DpKechengcode();
            tmp.setIsNewRecord(true);
            tmp.setKcid(dpKechengcode.getKcid());
            tmp.setKccode(code);
            tmp.setCodestatus("0");
            codes.add(tmp);
        }
        dpKechengcodeService.saveBatch(codes);
        return renderResult(Global.TRUE, text("保存观看码成功！"));
    }

    /**
     * 导出数据
     */
    @RequiresPermissions("dp:kccode:dpKechengcode:view")
    @RequestMapping(value = "exportData")
    public void exportData(DpKechengcode dpKechengcode, HttpServletResponse response) {
        List<DpKechengcode> list = dpKechengcodeService.findList(dpKechengcode);
        String fileName = "观看码" + DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";
        try (ExcelExport ee = new ExcelExport("观看码", DpKechengcode.class)) {
            ee.setDataList(list).write(response, fileName);
        }
    }

    /**
     * 下载模板
     */
    @RequiresPermissions("dp:kccode:dpKechengcode:view")
    @RequestMapping(value = "importTemplate")
    public void importTemplate(HttpServletResponse response) {
        DpKechengcode dpKechengcode = new DpKechengcode();
        List<DpKechengcode> list = ListUtils.newArrayList(dpKechengcode);
        String fileName = "观看码模板.xlsx";
        try (ExcelExport ee = new ExcelExport("观看码", DpKechengcode.class, Type.IMPORT)) {
            ee.setDataList(list).write(response, fileName);
        }
    }

    /**
     * 导入数据
     */
    @ResponseBody
    @RequiresPermissions("dp:kccode:dpKechengcode:edit")
    @PostMapping(value = "importData")
    public String importData(MultipartFile file) {
        try {
            String message = dpKechengcodeService.importData(file);
            return renderResult(Global.TRUE, "posfull:" + message);
        } catch (Exception ex) {
            return renderResult(Global.FALSE, "posfull:" + ex.getMessage());
        }
    }

    /**
     * 删除数据
     */
    @RequiresPermissions("dp:kccode:dpKechengcode:edit")
    @RequestMapping(value = "delete")
    @ResponseBody
    public String delete(DpKechengcode dpKechengcode) {
        DpKechengcode data = dpKechengcodeService.get(dpKechengcode.getId());
        if (data != null) {
            String codeStatus = data.getCodestatus();
            if ("1".equals(codeStatus)) {
                return renderResult(Global.FALSE, "该课程码已出售，不能删除");
            }
            if ("2".equals(codeStatus)) {
                return renderResult(Global.FALSE, "该课程码已被订单锁定，不能删除");
            }
            dpKechengcodeService.delete(dpKechengcode);
        }
        return renderResult(Global.TRUE, text("删除观看码成功！"));
    }

}